<?php
/**
 *  菜单表模型
 * Created by PhpStorm.
 * User: qingyun
 * Date: 17/4/27
 * Time: 下午2:17
 */
namespace Common\Model;
use Think\Model;

class MenuModel extends Model{
    //  无表前缀的菜单表表名
    private $table;
    //  有表前缀的菜单表表名
    private $menuTable;
    //  表前缀
    private $prefix;

    /**
     * MenuModel constructor.
     * @param string $table
     * @param string $prefix
     */
    public function __construct($table,$prefix = ''){
        parent::__construct();
        $this -> prefix = empty($prefix) ? C('DB_PREFIX') : $prefix;
        $this -> table = $table;
        $this -> menuTable = $this -> prefix . $this -> table;
    }

    /**
     * 添加菜单(修改)
     * @param $data
     * @return array|bool
     */
    public function addMenu($data){
        $checkResult = $this -> checkData($data);
        if(true !== $checkResult){
            return $checkResult;
        }
        $data = $this -> formatData($data);

        $menu = M($this -> table);
        $isOk = $menu -> add($data,[],true);
        if(!$isOk){
            return $this -> returnInfo('操作失败');
        }
        return $this -> returnInfo('操作成功',0);
    }

    /**
     * 更改菜单状态
     * @param $id
     * @return array
     */
    public function changeStatus($id){
        if(empty($id) || $id == 1){
            return $this -> returnInfo('非法操作');
        }

        $menu = M($this -> table);
        $r = $menu ->field('status') -> where(['id' => $id]) -> find();
        if(1 == $r['status']){
            $menu -> where(['id' => $id]) -> setField('status', 0);
            return $this -> returnInfo('已禁用',0,0);
        }else{
            $menu -> where(['id' => $id]) -> setField('status', 1);
            return $this -> returnInfo('已开启',0,1);
        }
    }

    /**
     *  删除
     * @param $id
     * @return array
     */
    public function del($id){
        if(empty($id)){
            return $this -> returnInfo('非法操作');
        }
        //  查询是否有子菜单
        $menuCount = M($this -> table)->where(['pid' => $id])->count();
        if($menuCount > 0){
            return $this -> returnInfo('请先删除子权限');
        }
        $isDel = M($this -> table)->where(['id' => $id])->delete();
        if(!$isDel){
            return $this -> returnInfo('删除失败');
        }
        return $this -> returnInfo('删除成功',0);
    }

    /**获取数据列表
     * @param $id
     * @param string $field
     * @param string $order
     * @return array
     */
    public function menuList($id,$field='id',$order = 'px DESC'){
        $menu = M($this->table);
        $list = $menu->field($field);
        if(!empty($id)){
            $where['id'] = $id;
            $list = $list->where($where);
        }
        $list = $list->order($order)->select();
        if(empty($list)){
            return $this->returnInfo('没有找到！',1,'');
        }else{
            return $this->returnInfo('成功！',0,$list);
        }
    }

    /**
     * 获取登录用户权限 对应的菜单列表
     * @return array
     */
    public function getNavList(){
        $data = M($this -> table) -> field('id,pid,title,link') -> select();
        if(1 == session('user.id')){
            return make_tree($data);
        }
        $auth = new \Think\Auth();
        foreach($data as $k => $v){
            if(!$auth -> check($v['link'], session('user.uid'))){
                unset($data[$k]);
            }
        }
        return make_tree($data);
    }

    /**
     * 数据格式验证
     * @param $data
     * @return array|bool
     */
    public function checkData($data){
        if(empty($data['title'])){
            return $this -> returnInfo('菜单名称不能为空');
        }
        if(empty($data['link'])){
            return $this -> returnInfo('菜单链接不能为空');
        }
        if(!empty($data['px']) && (strlen($data['px']) > 5) ){
            return $this -> returnInfo('排序格式不符合要求');
        }
        //  id不为空时为修改   此时不验证菜单是否重复
        if(empty($data['id'])){
            $menu = M($this -> table) -> where(['title' => $data['title'], 'link' => $data['link']]) -> find();
            if($menu > 0){
                return $this -> returnInfo('菜单不能重复');
            }
        }
        return true;
    }

    /**
     * 格式化过滤数据
     * @param $data
     * @return array
     */
    public function formatData($data){
        $id = empty($data['id']) ? 0 : $data['id'];
        $pid = empty($data['pid']) ? 0 : $data['pid'];
        $title = empty($data['title']) ? 0 : $data['title'];
        $link = empty($data['link']) ? 0 : $data['link'];
        $px = empty($data['px']) ? 0 : $data['px'];
        $status = empty($data['status']) ? 0 : $data['status'];
        $type = empty($data['type']) ? 1 : $data['type'];

        $datas = ['id' => $id, 'pid' => $pid, 'title' => $title, 'link' => $link, 'px' => $px, 'status' => $status, 'type' => $type];
        return $datas;
    }

    /**
     * 统一返回处理信息
     * @param $msg
     * @param int $status
     * @param string $data
     * @return array
     */
    public function returnInfo($msg, $status = 1, $data = ''){
        return ['status' => $status, 'msg' => $msg, 'data' => $data];
    }
}